package com.example.demo.controller;

import com.example.demo.entity.Files;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/file")//给该类的所有路径添加前缀
public class FileController {
    @GetMapping("downloadFile")
    private List<Files> getFile(@RequestParam(defaultValue = "D:/download") String path) throws UnknownHostException {
        File file = new File(path);
        File[] files = file.listFiles();
        List<Files> filesList = new ArrayList<>();
        String ipAddress = InetAddress.getLocalHost().getHostAddress();//获取当前机子的IP地址
        for (int i = 0; i < files.length; i++) {
            File item = files[i];
            Files filess = new Files();
            filess.setFilename(item.getName());
            filess.setPath(item.getAbsolutePath());
            filess.setUrl("");
        }
        return filesList;
    }
}
